<template>
  <div>
    <el-dialog
      class="fileSynchronization-detail"
      :title="title"
      :visible.sync="dialogVisible"
      width="70%"
      :before-close="cancel"
      @open="open"
      @close="closeDialog"
    >
      <div class="model-view" :key="ke">
        <div class="content-view">
          <div slot="title" class="title-view">
            <span class="title-line"></span>原始数据
          </div>
          <div style="width: 100%; height: 350px">
            <!-- <CleanForm
              :showColumnHandle="false"
              :searShow="false"
              :showColumnSetting="false"
              :showCheckbox="false"
              :showIndex="true"
              :showExportBtn="false"
              :field-list="fieldList"
              :pageSize="5"
              :api="queryRawDataDetail"
              :queryParam="maintenanceForm"
              tableOptionWidth="100"
              ref="maintenanceFormTable"
              :page="false"
              :dialogPage="true"
              :btnShow="true"
              :outLoad="false"
            >
      </CleanForm> -->

            <el-table
              :data="
                tableData.slice(
                  (currentPage - 1) * pageSize,
                  pageSize * currentPage
                )
              "
              border
              style="width: 100%;height: 100%;"
              height="350"
            >
              <el-table-column prop="time" label="时间" align="center">
              </el-table-column>
              <el-table-column prop="p_value" label="有功/kW" align="center">
              </el-table-column>
              <el-table-column prop="f_value" label="频率/Hz" align="center">
              </el-table-column>
            </el-table>

            <div class="feny">
              <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="currentPage"
                :page-sizes="[5,10,15,20]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
              >
              </el-pagination>
            </div>
          </div>
          <div>
            <div slot="title" class="title-view">
              <span class="title-line"></span>P-F曲线
            </div>
            <div id="main" style="width: 100%; height: 300px" ref="main"></div>
          </div>
        </div>

        <!-- <div class="content-view" v-if="!detailObj">
          <div v-loading="queryLoading" style="height: 500px"></div>
        </div> -->
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel" type="primary">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getMaintenanceList } from '@/api/maintenanceRecord'
import { queryRawDataDetail } from '@/api/detail'
import { number } from 'echarts'

export default {
  name: 'detail',
  props: {
    ke: {
      type: number,
      default: () => 1,
    },
    fileId: {
      type: String,
      default: () => '1',
    },
    dialogVisible: {
      type: Boolean,
      default: () => false,
    },
    diglogObj: {
      type: Object,
      default: () => {},
    },
    title: {
      type: String,
      default: '',
    },
    typeStr: {
      type: String,
      default: '',
    },
  },
  components: {},
  data() {
    return {
      YGData: [],
      PLData: [],
      xtime: [],
      currentPage: 1, // 当前页数，
      pageSize: 5, // 每一页显示的条数
      total: 20, // 总的条数
      tableData: [],
      maintenanceForm: {
        fileId: this.fileId,
      },
      queryRawDataDetail,
      chartDom: '',
      myChart: '',
      option: '',
      descriptionsKey: false,
      fieldList: [
        {
          name: '时间',
          value: 'time',
          noSort: true,
        },
        { name: '有功/kW', value: 'p_value', noSort: true },
        { name: '无功/kW', value: 'f_value', noSort: true },
      ],
    }
  },
  mounted() {
    // this.$nextTick(() => {
    //     // this.$refs.maintenanceFormTable.getDataInit()
    //   //   this.chartDom = document.getElementById("main");
    //   //   this.myChart = echarts.init(this.chartDom);
    //   this.initChart();
    //   this.queryTableData();
    //   })
  },
  methods: {
    initChart(e) {
      let myChart = this.$echarts.init(document.getElementById('main'))
      let option = {
        title: {
          text: '',
        },
        tooltip: {
          trigger: 'axis',
        },
        legend: {
          data: ['有功/kW', '频率/Hz'],
        },
        grid: {
          left: '2%',
          right: '2%',
          bottom: '0%',
          containLabel: true,
        },
        // toolbox: {
        //   feature: {
        //     saveAsImage: {}
        //   }
        // },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: this.xtime,
        },
        yAxis: [
          {
            type: 'value',
            name: '有功/kW',
            position: 'left',
            // axisLabel: {
            //     formatter: '{value} °C'
            //   }
          },
          {
            type: 'value',
            position: 'right',
            name: '频率/Hz',
            // axisLabel: {
            //     formatter: '{value} °C'
            //   }
          },
        ],
        series: [
          {
            name: '有功/kW',
            type: 'line',
            stack: 'Total',
            data: this.YGData,
            yAxisIndex: 0,
          },
          {
            name: '频率/Hz',
            type: 'line',
            stack: 'Total',
            data: this.PLData,
            yAxisIndex: 1,
          },
        ],
      }
      myChart.clear()
      this.myCharts = this.$echarts.init(document.getElementById('main'))
      this.myCharts.setOption(option)
      window.addEventListener('resize', () => {
        this.myCharts.resize()
      })
    },

    queryTableData() {
      console.log(this.fileId)
      queryRawDataDetail({ fileId: this.fileId })
        .then((res) => {
          if (res.code === '000000') {
            this.tableData = res.data.records
            this.total = res.data.records.length

            for (let i = 0; i < res.data.records.length; i++) {
              this.YGData.push(res.data.records[i].p_value)
              this.PLData.push(res.data.records[i].f_value)
              this.xtime.push(res.data.records[i].time)
            }
            this.initChart()
            // this.detailList = tabledetail[str];
            // this.tableOtherDetailList = tableOtherDetail[str];
            // this.tableZCDetailList = tableZCDetail[str];
            // this.detailObj = res.data.data;
            // this.descriptionsKey++;
          }
          // this.queryLoading = false;
          // this.tableDataLoading = false;
        })
        .catch((err) => {
          console.log(err)
          // this.queryLoading = false;
          // this.tableDataLoading = false;
        })
    },

    /**
     * @Description: 打开dialog
     * @Author: 赵程成
     */
    open() {
      // this.queryData();
      // this.initChart();
      // console.log('11111')
      this.queryTableData()
    },
    /**
     * @Description: 关闭dialog
     * @Author: 赵程成
     */
    closeDialog() {
      this.detailObj = null
      this.queryLoading = true
      this.dialogVisible = false
      this.$emit('close', false)
      this.YGData = []
      this.PLData = []
      this.xtime = []
      // 销毁echarts实例
    },
    resetForm() {
      this.$refs['form'].resetFields()
    },
    cancel() {
      this.$emit('close', false)
    },
    handleSizeChange(e) {
      this.currentPage = 1
      this.pageSize = e
      // this.queryData();
    },
    handleCurrentChange(e) {
      this.currentPage = e
      // this.queryData();
    },
  },
}
</script>

<style lang="scss" scoped>
.feny {
  display: flex;
  justify-content: center;
}

.model-view {
  width: 100%;
  height: 100%;
  background-color: white;

  overflow-y: auto;
  // border-bottom: 1px solid #eee;

  .title-view {
    display: flex;
    align-items: center;
    .title-line {
      width: 4px;
      height: 16px;
      background: #3c6cfe;
      margin-right: 8px;
    }
  }
  .content-view {
    padding-bottom: 24px;
    .el-title {
      border-bottom: none;
      height: 30px;
      color: #3c6cfe;
    }
  }
}
</style>
